﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Html;
using System.Text;
using EzDesk.Core.Model;
using EzDesk.Utility.Generic;
using EzDesk.Utility.Web.ServiceReference;
using Microsoft.Practices.Unity;
using EzDesk.Clients.MobileWeb.CoreClientService;
using EzDesk.Clients.MobileWeb.Controllers;

namespace EzDesk.Clients.MobileWeb.Extensions
{
    public static class HtmlHelperExtensions
    {
        public static string ReturnButton(this HtmlHelper html)
        {
            string returnUrl = html.ViewData["r"] as String;
            if (String.IsNullOrEmpty(returnUrl))
            {
                string controllerObjName = html.ViewContext.Controller.GetType().Name;
                string controllerName = controllerObjName.Remove(controllerObjName.Length - 10);
                
                //todo: 跳转到相应 Controller 的Index页面 html.ViewContext.Controller
                //跳转到首页
                html.ActionLink("返回", "Index", controllerName);
            }
            return String.Format(
                "<a href = \"{0}\">返回</a>",
                returnUrl
                );
        }

        //todo: 使用AppFunction的API重构
        [Obsolete]
        public static string NavigationTop(this HtmlHelper html, Guid currentUserId)
        {
            var coreClientService = ServiceReferenceFactory<ICoreClientService, CoreClientServiceClient>
                .GetServiceWithUseragentStr("EzDesk,GenericcMobile");

            var functionTypes = coreClientService.GetAllAppFunctionTypes(currentUserId);
            string clientBaseUrl = coreClientService.GetClientBaseUrl();

            var htmlBuilder = new StringBuilder();
            // begin
            htmlBuilder.Append("<div id=\"mainnav\" class=\"nav sec\">");

            // 我的 EzDesk
            addNavItem("我的EzDesk", "/Home/Index", ref htmlBuilder);

            // 加入各个 AppFunction
            foreach (var functionType in functionTypes)
            {
                addNavItem(functionType.DisplayText, string.Format("http://" + clientBaseUrl + "/Home/AppFunction/{0}", functionType.TypeName), ref htmlBuilder);
            }

            // 注销按钮
            addNavItem("注销", "http://" + clientBaseUrl + "/Account/LogOff", ref htmlBuilder, true);
            // end
            htmlBuilder.Append("</div>");

            return htmlBuilder.ToString();
        }

        private static void addNavItem(string text, string href, ref StringBuilder htmlBuilder, bool isLast = false)
        {
            htmlBuilder.AppendFormat("<a href=\"{0}\">{1}</a>", href, text);
            if (!isLast)
            {
                htmlBuilder.AppendFormat("|");
            }
        }



        public static string SimpleUserLabel(this HtmlHelper html, SimpleUser user, bool displayDept = false)
        {
            // todo: 在SimpleUser加入Dept内容后修改
            if (displayDept)
            {
                return
                    string.Format(
                        "<a href=\"{0}\">{1}</a>(<a href=\"{2}\">{3}</a>)",
                        user.Id.ToString(),
                        user.Name,
                        user.Dept.Id,
                        user.Dept.DeptName
                        );
            }
            else
            {
                return string.Format("<a href=\"#{0}\">{1}</a>", user.Id.ToString(), user.Name);
            }
        }

        public static string SimpleGroupLabel(this HtmlHelper html, Group group)
        {
            return string.Format("<a href=\"#{0}\">{1}</a>", group.Id.ToString(), group.Description);
        }
    }
}
